package com.yyfax.pay.common.util;

import org.apache.commons.lang.math.NumberUtils;

import com.google.common.base.Joiner;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.order.model.vo.PayBusiIdVO;

/**
 * 生成id和解析id工具类
 * 
 * @author maojian
 * @date Apr 26, 2017
 * @since V1.0.0
 */
public class CommonIdUtils {
	/** 代付 **/
	private final static String PAYOUT_PREFIX = "XO";
	/** 批量代付 **/
	private final static String BATCH_PAYOUT_PREFIX = "BO";
	/** 代扣 **/
	private final static String PAYIN_PREFIX = "XI";
	/** 批量代扣 **/
	private final static String BATCH_PAYIN_PREFIX = "BI";
	/** 支付 **/
	private final static String PAY_PRFIX = "XP";
	/** 支付订单最大长度*/
	public static final int PAY_ORDER_MAX_LENGTH = 14;
	/** 鉴权 **/
	private final static String AUTH_PREFIX = "AU";

	/**
	 * 获取业务主键
	 * 
	 * @author maojian
	 * @date 2017-04-15 14:01:56
	 * @since v1.0.0
	 * @param merchantCode
	 * @param orderId
	 * @return
	 */
	public static String generateId(PayTypeEnum payType, String merchantCode, String orderId) {
		String prefix = null;
		if (payType.getValue().equals(PayTypeEnum.PAY.getValue())) {
			prefix = PAY_PRFIX;
		} else if (payType.getValue().equals(PayTypeEnum.AUTH.getValue())) {
			prefix = AUTH_PREFIX;
		} else if (payType.getValue().equals(PayTypeEnum.PAY_IN.getValue())) {
			prefix = PAYIN_PREFIX;
		} else if (payType.getValue().equals(PayTypeEnum.PAY_OUT.getValue())) {
			prefix = PAYOUT_PREFIX;
		} else if (payType.getValue().equals(PayTypeEnum.BATCH_PAY_IN.getValue())) {
			prefix = BATCH_PAYIN_PREFIX;
		} else if (payType.getValue().equals(PayTypeEnum.BATCH_PAY_OUT.getValue())) {
			prefix = BATCH_PAYOUT_PREFIX;
		}
		if (null == prefix) {
			return prefix;
		}
		return Joiner.on("").join(prefix, merchantCode, orderId);
	}

	/**
	 * 通过业务主键获取商户号和订单号的对象
	 * 
	 * @author maojian
	 * @date 2017-04-15 14:01:56
	 * @since TODO
	 * @param id
	 * @return PayBusiIdVO
	 */
	public static PayBusiIdVO getPayBusiIdVo(String id) {
		if (id.length() < 6) {
			return null;
		}
		return new PayBusiIdVO(id.substring(2, 6), id.substring(6));
	}

	public static void main(String[] args) {
		// System.out.println(generateId(PayTypeEnum.PAY,"8888","123456789"));
		// System.out.println(getPayBusiIdVo("XP8888123456789"));
		System.out.println(NumberUtils.isDigits("aba123".substring(2)));
	}
}
